﻿using System;
using System.Collections.Generic;
using System.Linq;
using CNav.Common;
using CNav.DataEntity;
using CNav.Domain;

namespace CNav.Business.Services
{
    public interface ISearchService
    {
        ResponseData<List<Consumer>> GetConsumerByFirstName(string text);

        ResponseData<List<Consumer>> GetConsumerByLastName(string text);

        ResponseData<List<Consumer>> GetConsumerByBirthDate(string text);

        ResponseData<List<Consumer>> GetConsumerByEmail(string text);

        ResponseData<List<Consumer>> GetConsumerByPhone(string text);

        ResponseData<List<Consumer>> GetConsumerByPostCode(string text);

        ResponseData<Consumer> GetConsumerByID(int id);
    }

    public class SearchService : ServiceBase, ISearchService
    {
        public ResponseData<List<Consumer>> GetConsumerByFirstName(string text)
        {
            var response = new ResponseData<List<Consumer>>();
            var result = Context.Get<ConsumerDto>(c => c.FirstName.Contains(text)).ToList();
            
            response.Data = ToConsumerList(result);

            return response;
        }
        
        public ResponseData<List<Consumer>> GetConsumerByLastName(string text)
        {
            var response = new ResponseData<List<Consumer>>();
            var result = Context.Get<ConsumerDto>(c => c.LastName.Contains(text)).ToList();
            response.Data = ToConsumerList(result);

            return response;
        }

        public ResponseData<List<Consumer>> GetConsumerByBirthDate(string text)
        {
            var dateTime = DateTime.Parse(text);
            var start = dateTime;
            var end = dateTime.AddDays(1);

            var response = new ResponseData<List<Consumer>>();
            var result = Context.Get<ConsumerDto>(c => c.BirthDate >= start && c.BirthDate< end).ToList();
            response.Data = ToConsumerList(result);

            return response;
        }

        public ResponseData<List<Consumer>> GetConsumerByEmail(string text)
        {
            var uText = text.ToUpper();
            var response = new ResponseData<List<Consumer>>();
            var result = Context.Get<ConsumerDto>(c => c.Email.Contains(text)).ToList();
            response.Data = ToConsumerList(result);

            return response;
        }

        public ResponseData<List<Consumer>> GetConsumerByPhone(string text)
        {
            var response = new ResponseData<List<Consumer>>();
        
            var result = Context.Get<ConsumerDto>(c => c.PhoneNumber == text).ToList();
            response.Data = ToConsumerList(result);

            return response;
        }

        public ResponseData<List<Consumer>> GetConsumerByPostCode(string text)
        {
            var response = new ResponseData<List<Consumer>>();
        
            var result = Context.Get<ConsumerDto>(c => c.Address.PostalCode.Contains(text)).ToList();
            response.Data = ToConsumerList(result);

            return response;
        }

        public ResponseData<Consumer> GetConsumerByID(int id)
        {
            var response = new ResponseData<Consumer>();

            var consumerDto = Context.Get<ConsumerDto>(c => c.ID == id).ToList().FirstOrDefault();
            if (consumerDto!= null)
            {
                response.Data = new Consumer
                  {
                      ID = consumerDto.ID,
                      Title = consumerDto.Title,
                      FirstName = consumerDto.FirstName,
                      MiddleName = consumerDto.MiddleName,
                      LastName = consumerDto.LastName,
                      BirthDate = consumerDto.BirthDate,
                      Email = consumerDto.Email,
                      PhoneNumber = consumerDto.PhoneNumber,
                      FaxNumber = consumerDto.FaxNumber,
                      AdminComment = consumerDto.AdminComment,
                      CreatedOnUtc = consumerDto.CreatedOnUtc,
                      LastActivityDateUtc = consumerDto.LastActivityDateUtc,
                      AddressID = consumerDto.AddressID
                  };

                return response; 
            }

            response.Error = new[] {"Invalid consumer id provided."};
            return response;
        }

        private static List<Consumer> ToConsumerList(IEnumerable<ConsumerDto> result)
        {
            return result.Select(p => new Consumer
            {
                ID = p.ID,
                Title = p.Title,
                FirstName = p.FirstName,
                MiddleName = p.MiddleName,
                LastName = p.LastName,
                BirthDate = p.BirthDate,
                Email = p.Email,
                PhoneNumber = p.PhoneNumber,
                FaxNumber = p.FaxNumber,
                AdminComment = p.AdminComment,
                CreatedOnUtc = p.CreatedOnUtc,
                LastActivityDateUtc = p.LastActivityDateUtc,
                AddressID = p.AddressID
            }).ToList();
        }
    }
}